<?php

$theme = "Initial ";
$ver = "2.3";

define('TPDIR', get_bloginfo('template_directory'));

// 谷歌字体库设置
if( get('ini_font')=="禁用字体" || !get('ini_font') ){
	add_filter('gettext_with_context','disable_open_sans', 888, 4);
} else {
	ob_start("ini_360_font");
}

// 禁用Open Sans
function disable_open_sans($turn,$text,$context ){
	if( 'Open Sans font: on or off'==$context && 'on'==$text ){
		$turn = 'off';
	}
	return $turn;
}
// 360代理字体
function ini_360_font($content){
	return str_replace('googleapis.com', 'useso.com', $content);
}

// 注册导航菜单
register_nav_menus( array('primary' => '导航菜单','mobile'=>'手机导航') );
function nav_fallback(){  #导航回调
	echo '<ul id="menu"><li><a href="/wp-admin/nav-menus.php?action=locations">设置导航菜单</a></li></ul>';
}

// 判断IE浏览器版本
function isIE8(){
	$userAgent = strtolower( $_SERVER["HTTP_USER_AGENT"] );
	if( ereg("msie 8",$userAgent) || ereg("msie 7",$userAgent) ) return true;
}

// 使用Mobile_Detect进行判断
include 'Mobile_Detect.php';
$detect = new Mobile_Detect();
function ismobile(){
	global $detect;
	if( $detect->isMobile() && !$detect->isTablet() && get('ini_mobile') ) return true;
}

// 公告
function ini_bulletin_post(){
	$labels = array(
		'name' => _x('公告', 'post type general name'),
		'singular_name' => _x('公告', 'post type singular name'),
		'add_new' => _x('添加新公告', '公告'),
		'add_new_item' => __('添加新公告'),
		'edit_item' => __('编辑公告'),
		'new_item' => __('新的公告'),
		'view_item' => __('预览公告'),
		'search_items' => __('搜索公告'),
		'not_found' =>  __('您还没有发布公告'),
		'not_found_in_trash' => __('回收站中没有公告'),
		'parent_item_colon' => ''
	);
	$args = array(
		'labels'=> $labels,
		'public' => true,
		'publicly_queryable' => true,
		'show_ui' => true,
		'query_var' => true,
		'rewrite' => false,
		'capability_type' => 'post',
		'has_archive' => true,
		'show_in_menu' => true,
		'show_in_nav_menus' => true,
		'hierarchical' => false,
		'menu_position'=> 5 ,
		'supports' => array( 'title', 'author', 'excerpt', 'thumbnail', 'trackbacks', 'editor',
 'comments', 'custom-fields', 'revisions' )
	);
register_post_type( 'bulletin', $args );
}
function ini_bulletin_rewrites(){
	add_rewrite_rule(
		'bulletin/([0-9]+)?.html$',
		'index.php?post_type=bulletin&p=$matches[1]',
		'top'
	);
}
function ini_bulletin_link( $link, $post = 0 ){
	if( $post->post_type == 'bulletin' ){
		return home_url( 'bulletin/'.$post->ID.'.html' );
	} else {
		return $link;
	}
}
if( !get('ini_custom_bulletin') && get('ini_bulletin') ){ #添加撰写公告并修改链接
	add_action( 'init', 'ini_bulletin_post' );
	add_action( 'init', 'ini_bulletin_rewrites' );
	add_filter( 'post_type_link', 'ini_bulletin_link', 1, 3 );
}

// RGBA颜色转换
function hex2rgba( $colour, $alpha=1 ){
	if( $colour[0] == '#' ) $colour = substr( $colour, 1 );   #去掉首部#号
	if( strlen($colour) == 6 ){
		list( $r, $g, $b ) = array($colour[0].$colour[1], $colour[2].$colour[3], $colour[4].$colour[5] );
	} elseif( strlen($colour) == 3 ){
		list( $r, $g, $b ) = array($colour[0].$colour[0], $colour[1].$colour[1], $colour[2].$colour[2] );
	} else {
		return false;
	}
	$r = hexdec( $r );
	$g = hexdec( $g );
	$b = hexdec( $b );
	$a = $alpha;
	return "rgba($r,$g,$b,$a)";
}


// 二维码
define ('INI_QRCHACHE', dirname(__FILE__) . "/../cache/"); //二维码缓存位置

//生成二维码图片
function ini_QR( $value, $size = 5 ){
	$errorCorrectionLevel = 'M'; //容错级别
	$cache = MD5( $value );
	if( !file_exists(INI_QRCHACHE . $cache . '-QR.png') ) { //判断文件是否存在
		require_once 'phpqrcode.php';
		QRcode::png($value, INI_QRCHACHE . $cache . '-QR.png', $errorCorrectionLevel, $size, 2);
	}
	return "<img src=" .TPDIR. "/cache/{$cache}-QR.png alt=QRcode />";
}

// 删除二维码
function ini_DelQR(){
	$files = glob( INI_QRCHACHE . '/*-QR.png');
	if ($files) {
		foreach($files as $file){
			@unlink($file);
		}
		return "二维码缓存清理完毕！";
	}
	else {
		return "暂无二维码缓存图片";
	}
}

// 添加虾米音乐支持，来自Hermit
if( get("ini_media") ){
	require_once "class.json.php";
	if(!isset($INIJSON)) $INIJSON = new InitialJson();
}

